package com.sx.email.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sx.email.EmailService;
import com.sx.email.Vo.MailVo;
import com.sx.email.service.MailSender;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author shengx
 * @date 2020/5/10 21:10
 */
@Service  // dubbo serivce
public class EmailController implements EmailService {

    @Autowired
    private MailSender mailSender;
    @SentinelResource(value = "email", fallback = "emailFallback")
    public Boolean email(String email, String code){
        MailVo mailVo = new MailVo();
        mailVo.setFrom("243725821@qq.com");
        mailVo.setTo(email);
        mailVo.setId("1");
        mailVo.setSubject("验证码");
        mailVo.setText(code);
        MailVo mailVo1 = mailSender.sendMail(mailVo);
        System.out.println(mailVo1);
        System.out.println("email:" + email + " code:" + code);
        if(mailVo1.getStatus().equals("OK")){
            return true;
        }
        return false;
    }

    // 发送email的fallback方法
    public Boolean emailFallback(String email, String code){
        System.out.println("email:" + email + " fallback!!");
        return false;
    }
}
